<? include ("../init.php") ?>
<? include ("../includes/class.upload.php") ?>
<?
  $catalogue = $_POST['catalogue'];
  $name = addslashes($_POST['name']);
  
  if(trim($_POST['date'])!=''){
    $date = $_POST['date'];
  }else{
    $date = date('Y-m-d');
  }
  
  $area = addslashes($_POST['area']);
  $owner = addslashes($_POST['owner']);
  $year = $_POST['year'];
  //$value = str_replace(',','',$_POST['value']);
  $value = $_POST['value'];
  
  $sql = "INSERT INTO projects VALUES('','$catalogue','$date','$name','$area','$owner','$year','$value','1','','')";
  $db->execute($sql);
  $project_id = $db->lastInsertedId();
  $_SESSION['ADDPROJECTID'] = $project_id;
  
  if(isset($_FILES["img"]) && is_uploaded_file($_FILES["img"]["tmp_name"]) && $_FILES["img"]["tmp_name"]!=''){
    $handle = new Upload($_FILES['img']);
    if ($handle->uploaded)
    {
      $handle->image_resize = true;
      $handle->image_ratio_fill = true;
      $handle->image_x = 113;
      $handle->image_y = 75;
      $handle->Process('../../images/projects/'.$project_id);
      
      if ($handle->processed)
      {
        $sql = "UPDATE projects SET project_img ='".$handle->file_dst_name."' WHERE project_id=$project_id";
         if(!mysql_query($sql))
         {
            @unlink('../../images/projects/'.$project_id.'/'.$handle->file_dst_name);
         }
         else
         {
          $handle->image_resize = true;
          $handle->image_ratio_fill = true;
          $handle->file_name_body_pre = 'big_';
          $handle->image_x = 350;
          $handle->image_y = 260;
          $handle->Process('../../images/projects/'.$project_id);
         }
      }
      $handle->clean();
    }
  }
  
  if(isset($_FILES["cert"]) && is_uploaded_file($_FILES["cert"]["tmp_name"]) && $_FILES["cert"]["tmp_name"]!='')
  {
    $handle = new Upload($_FILES['cert']);
    if ($handle->uploaded)
    {
        $handle->Process('../../images/projects/'.$project_id);
        
        if ($handle->processed)
        {
          $sql = 'UPDATE projects SET project_cert ="'.$handle->file_dst_name.'" WHERE project_id='.$project_id;
           if(!mysql_query($sql))
           {
              @unlink('../../images/projects/'.$project_id.'/'.$handle->file_dst_name);
           }
        }
        $handle->clean();
    }
  }
  
  echo <<<js
    <script>
      top.window.uploadify_({$project_id});
    </script>
js;
?>